<?php
namespace Cart;
class Check {
	private $data = array();

	public function __construct($registry)
    {
        $this->db = $registry->get('db');
        $this->config = $registry->get('config');
        $this->customer = $registry->get('customer');
        $this->session = $registry->get('session');
        $this->request = $registry->get('request');
    }

    /**
     * 检查token是否有效
     * @return bool
     */
    public function checkToken(){
	    if(isset($_REQUEST['api_token']) && $_REQUEST['api_token']){
            $session_id = $_REQUEST['api_token'];
        }else{
            return false;
        }
        $cart_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "session` WHERE session_id = '" . $session_id."'");
        if ($cart_query->num_rows) {
            $expire = $cart_query->row['expire'];
            $data = json_decode($cart_query->row['data'],true);
            if(time() <= strtotime($expire) && isset($data['api_id'])){//api_id存在并且未过期
                return true;
            }else{
                return false;
            }
        }
        return false;
    }

    public function shopSignEncrypt($key, $data = [])
    {
        unset($data['sign']);
        ksort($data);
        $string = http_build_query($data);
        return md5($string . $key);
    }
}
